
using System;
using System.Reflection;

namespace Serpis
{
	public static class MemberInfoExtensions
	{
		public static TAttribute GetAttribute<TAttribute>(this MemberInfo memberInfo) 
			where TAttribute : Attribute
		{
			return (TAttribute)Attribute.GetCustomAttribute(memberInfo, typeof(TAttribute), true);
		}
		
		public static bool IsDefined<TAttribute>(this MemberInfo memberInfo)
		{
			return Attribute.IsDefined(memberInfo, typeof(TAttribute), true);
		}

		public static string GetLabel(this MemberInfo memberInfo)
		{
			if (!memberInfo.IsDefined<LabelAttribute>())
				return memberInfo.Name;
			LabelAttribute labelAttribute = memberInfo.GetAttribute<LabelAttribute>();
			return labelAttribute.Label;
		}
	}
}
